package com.apes.fn.scm.settleCheck.model;

import com.alibaba.fastjson.annotation.JSONField;
import com.apes.fn.scm.contract.FnContract;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.Name;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.util.ApesConst;
import com.apes.framework.util.DateUtil;
import com.apes.scm.masterdata.frame.model.Dept;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

/**
 * 最原始的对账单服务，已经不在使用，仅保留查询逻辑
 * 功能：赋能商户结算对账单  主表
 *
 * @author zhuyh
 * @time 14:39
 * @date 2020/3/6
 */
@Deprecated
@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = RclPartnerAccountStatement.CONST_TABLE_NAME)
public class RclPartnerAccountStatement extends Model {


    public static final String CONST_TABLE_NAME = "fn_partner_account_statement";

    @Id
    @Column(name = "id", nullable = false, length = 24)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "赋能结算对账单号", padding = 12)
    private String id;

    @Column(name = "name", length = 250)
    @Comment("结算对账单号名称")
    private String name;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "partner_check_id", referencedColumnName = "id", nullable = false)
    @Comment("决策单号")
    private PartnerCheck partnerCheck;

    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note;

    @Column(name = "receivable", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("代收款总额")
    private double receivable = 0.0;  //

    @Column(name = "rclPayment", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("代支付总额")
    private double payment = 0.0;//

    @Column(name = "accountReceviable", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("应收平台费用总额")
    private double accountReceviable = 0.0;//


    @Column(name = "currentIncome", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("本期收益总额")
    private double currentIncome = 0.0;//


    @Column(name = "otherpayment", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("其他应收账款")
    private double otherpayment = 0.0;//


    @Column(name = "accounts_receivable", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("门店应收账款-平台内")
    private double accountsReceivable= 0.0;//


    @Column(name = "other_accounts_receivable", nullable = false, columnDefinition = ApesConst.FIELD_QTY_16_4_0)
    @Comment("其它应收账款")
    private double otherAccountsReceivable = 0.0;//


    @Column(name = "state")
    @Comment("状态(create:创建,commit:提交,recount:重新统计,back:退回,confirm:确认,clear:清账)")
    private String state;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "commit_uid", referencedColumnName = "id")
    @Comment("提交人")
    private PartyRole commitUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "commit_date")
    @Comment("提交日期")
    private Date commitDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "recount_uid", referencedColumnName = "id")
    @Comment("重新统计人")
    private PartyRole recountUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "recount_date")
    @Comment("重新统计日期")
    private Date recountDate;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "confirm_uid", referencedColumnName = "id")
    @Comment("确认人")
    private PartyRole confirmUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "confirm_date")
    @Comment("确认日期")
    private Date confirmDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "create_uid", referencedColumnName = "id")
    @Comment("创建人")
    private PartyRole createUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "create_date")
    @Comment("创建日期")
    private Date createDate;

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "back_uid", referencedColumnName = "id")
    @Comment("退回人")
    private PartyRole backUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "back_date")
    @Comment("退回日期")
    private Date backDate;


    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "clear_uid", referencedColumnName = "id")
    @Comment("清账人")
    private PartyRole clearUid;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "clearDate")
    @Comment("清账日期")
    private Date clearDate;


    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "start_date")
    @Comment("对账开始日期")
    private Date startDate;

    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.DATE)
    @Column(name = "end_date")
    @Comment("对账结束日期")
    private Date endDate;
    //业务类型
    @OneToMany(mappedBy = "rclPartnerAccountStatement", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<RclBusinessClassification> rclBusinessClassifications = new HashSet();

    public Set<RclBusinessClassification> getRclBusinessClassifications() {
        for (RclBusinessClassification item : rclBusinessClassifications) {
            // 设置主表
            item.setRclPartnerAccountStatement(this);
        }
        return rclBusinessClassifications;
    }


    //其他待付款
    @OneToMany(mappedBy = "rclPartnerAccountStatement", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<RclOtherPayment> rclOtherPayments = new HashSet();

    public Set<RclOtherPayment> getRclOtherPayments() {
        for (RclOtherPayment item : rclOtherPayments) {
            // 设置主表
            item.setRclPartnerAccountStatement(this);
        }
        return rclOtherPayments;
    }



    //支付款汇总
    @OneToMany(mappedBy = "rclPartnerAccountStatement", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<RclPayment> rclPayments = new HashSet();

    public Set<RclPayment> getRclPayments() {
        for (RclPayment item : rclPayments) {
            // 设置主表
            item.setRclPartnerAccountStatement(this);
        }
        return rclPayments;
    }


    //支付方式
    @OneToMany(mappedBy = "rclPartnerAccountStatement", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<RclPaymentMethod> rclPaymentMethods = new HashSet();

    public Set<RclPaymentMethod> getRclPaymentMethods() {
        if (rclPaymentMethods ==null) return null;
        for (RclPaymentMethod item : rclPaymentMethods) {
            // 设置主表
            item.setRclPartnerAccountStatement(this);
        }
        return rclPaymentMethods;
    }


    //当期平台应收费用
    @OneToMany(mappedBy = "rclPartnerAccountStatement", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<RclPlatformReceivable> rclPlatformReceivables = new HashSet();

    public Set<RclPlatformReceivable> getRclPlatformReceivables() {
        for (RclPlatformReceivable item : rclPlatformReceivables) {
            // 设置主表
            item.setRclPartnerAccountStatement(this);
        }
        return rclPlatformReceivables;
    }

    @Name
    @Transient
    private PartyRole fnCustomer;  //商户

    public PartyRole getFnCustomer() {
        if (partnerCheck == null) return null;
        return partnerCheck.getFnCustomer();
    }

    @Transient
    private Dept saleDept;

    public Dept getSaleDept() {
        if (partnerCheck == null) return null;
        return partnerCheck.getSaleDept();
    }

    @Transient
    private FnContract fnContract;

    public FnContract getFnContract() {
        if (partnerCheck == null) return null;
        return partnerCheck.getFnContract();
    }
}
